package br.unicentro.lynx.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.unicentro.lynx.model.factory.ConnectionFactory;
import br.unicentro.lynx.model.pojo.Student;

public class StudentDao {
	private String sql;
	private Connection connection;
	private PreparedStatement statement;
	private ResultSet result;

	public void insert(Student student) {
		open();
		this.sql = "INSERT INTO Students (cgm_student, name_student, guardian_name_student, class_student) VALUES (?, ?, ?, ?)";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setString(1, student.getCgmStudent());
			this.statement.setString(2, student.getNameStudent());
			this.statement.setString(3, student.getGuardianNameStudent());
			this.statement.setString(4, student.getClassStudent());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void update(Student student) {
		open();
		this.sql = "UPDATE Students SET cgm_student = ?, name_student = ?, guardian_name_student = ?, class_student = ? WHERE id_student = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setString(1, student.getCgmStudent());
			this.statement.setString(2, student.getNameStudent());
			this.statement.setString(3, student.getGuardianNameStudent());
			this.statement.setString(4, student.getClassStudent());
			this.statement.setInt(5, student.getIdStudent());
			this.statement.execute();
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void delete(Student student) {
		open();
		this.sql = "DELETE FROM Students WHERE id_student = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, student.getIdStudent());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public Student search(Integer id_student) {
		open();
		this.sql = "SELECT * FROM Students WHERE id_student = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, id_student);
			this.result = this.statement.executeQuery();
			if (this.result.next()) {
				return new Student(result.getInt("id_student"),
						result.getString("cgm_student"),
						result.getString("name_student"),
						result.getString("guardian_name_student"),
						result.getString("class_student"));
			} else {
				return null;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public ArrayList<Student> list() {
		open();
		this.sql = "SELECT * FROM Students";
		ArrayList<Student> students = new ArrayList<Student>();
		try {
			this.statement = connection.prepareStatement(sql);
			this.result = this.statement.executeQuery();
			while (this.result.next()) {
				students.add(new Student(result.getInt("id_student"), result
						.getString("cgm_student"), result
						.getString("name_student"), result
						.getString("guardian_name_student"), result
						.getString("class_student")));
			}
			return students;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				this.result.close();
				close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	private void open() {
		this.connection = ConnectionFactory.getConnection(
				ConnectionFactory.URL, ConnectionFactory.USER,
				ConnectionFactory.PASSWORD);
	}

	private void close() {
		try {
			this.statement.close();
			this.connection.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
